package com.example.mall.controller.v1;

import com.example.mall.common.core.response.SuccessResponse;
import com.example.mall.common.core.exception.ParameterException;
import com.example.mall.common.core.exception.UnauthorizedException;
import com.example.mall.common.utils.SuccessHelper;
import com.example.mall.dto.token.TokenGetDTO;
import com.example.mall.service.PwdAuthenticationService;
import com.example.mall.service.WxAuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/token")
@Validated
public class TokenController {
    @Autowired
    private PwdAuthenticationService pwdAuthenticationService;
    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    /**
     *  用户认证授权
     *  - 颁发令牌
     * @return { token: "xxxx" }
     */
    @PostMapping
    public SuccessResponse getToken(@RequestBody @Validated TokenGetDTO userData) {
        // 处理不同登录
        String token = "";
        switch (userData.getLoginType()) {
            case USER_Email:
                break;
            case USER_PASSWORD:
                token = pwdAuthenticationService.getAccountAdnPassword(userData);
                break;
            case USER_MOBILE:
                break;
            case USER_WX:
                token = wxAuthenticationService.code2Session(userData.getAccount());
                break;
            default:
                throw new ParameterException(10003);
        }

        if ("".equals(token))
            throw new UnauthorizedException(10006);
        return SuccessHelper.excute("token", token);
    }

}
